var myApp = angular.module('myApp', [
    'ngAnimate',
    'ngResource',
    'ui.router',
    'angular-loading-bar',
    'templates']);
angular.module('templates',[]);
myApp.config(function($stateProvider, $urlRouterProvider,$httpProvider,$locationProvider) {
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    var param = function(obj) {
        var query = '', name, value, fullSubName, subName, subValue, innerObj, i;
        for(name in obj) {
            var demo = '';
            value = obj[name];
            if(value instanceof Array) {
                query += encodeURIComponent(name) + '=' + encodeURIComponent(angular.toJson(value)) + '&';
            }
            else if(value instanceof Object) {
                query += encodeURIComponent(name) + '=' + encodeURIComponent(angular.toJson(value)) + '&';
            }
            else if(value !== undefined && value !== null)
                query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
        }
        return query.length ? query.substr(0, query.length - 1) : query;
    };
    $httpProvider.defaults.transformRequest = [function(data) {
        return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
    /*默认路径*/
    $urlRouterProvider.otherwise("/home");

    /*$locationProvider.html5Mode({
        enabled: true,
        requireBase: true
    });*/
    $locationProvider.hashPrefix('!');

});
myApp.config(function  (cfpLoadingBarProvider, $httpProvider) {
    cfpLoadingBarProvider.includeSpinner = true;
    $httpProvider.interceptors.push('interceptor');
});
myApp.run(function ($rootScope) {
    var title = ' 农安县非公党建网';
    $rootScope.rootRouterTitle = '';
    $rootScope.rootTitle = $rootScope.rootRouterTitle+$rootScope.rootRouterTitle?' ':''+'';
    $rootScope.$on('$stateChangeSuccess',function () {
        window.scrollTo(0,0);
        if($rootScope.rootRouterTitle){
            $rootScope.rootTitle = $rootScope.rootRouterTitle+' - '+title;
        }else {
            $rootScope.rootTitle = title;
        }
    });
});
myApp.factory('interceptor', function( $injector,cfpLoadingBar) {
    return {
        'request': function(request) {
            cfpLoadingBar.start();
            return request;
        },
        'response': function(response) {
            cfpLoadingBar.complete();
            return response;
        },
        'requestError': function(rejection) {
            cfpLoadingBar.complete();
            return rejection;
        },
        'responseError': function(rejection) {
            cfpLoadingBar.complete();
            return rejection;
        }
    };
});
myApp.constant('URL',api_url)
    .constant('IMGURL',img_url)
    .filter('imgwhq',function(IMGURL){
        var url = IMGURL;
        return function(date,w,h,q) {
            var z = '';
            if(!!w&&!!h&&!!q){
                z = url+date+'@'+Number(w)+"w_"+Number(h)+'h_'+Number(q)+'q_1e_1c';//+date.match(/\.\w*?$/g)
                return z;
            }else {
                z = url+date;

            }
            return z;
        }
    });